﻿using System;

class BinarysearchInArray
{
    static void Main(string[] args)
    {
        Console.Write("Type number N: ");
        int numberN = int.Parse(Console.ReadLine());
        int[] array = new int[numberN];

        for (int i = 0; i < numberN; i++)
        {
            Console.Write("Element a[{0}]: ", i);
            array[i] = int.Parse(Console.ReadLine());
        }

        Console.WriteLine("Type number K: ");
        int numberK = int.Parse(Console.ReadLine());

        //int[] array = { 1, 3, 7, 9, 11, 18, 21, 26, 29, 41, 54, 68, 87, 88, 90 };
        //int numberK = 54;

        Array.Sort(array);
        int index = Array.BinarySearch(array, numberK);
        if (index>0)
        {
            Console.WriteLine("Largest number lesser or equal to K is: {0}",array[index]);
        }
        else
        {
            Console.WriteLine("Largest number lesser or equal to K is: {0}", array[-index - 2]);
        }
    }
}